package com.it.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.it.entity.PageResult;
import com.it.entity.QueryPageBean;
import com.it.pojo.CheckItem;
import com.it.constant.MessageConstant;
import com.it.entity.Result;
import com.it.service.CheckItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.swing.*;
import java.util.List;

/**
 * 体检检查项管理
 */
@RestController
@RequestMapping("/checkitem")
public class CheckItemController {
    @Reference
    private CheckItemService checkItemService;
    //新增
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")//权限校验
    @RequestMapping("/add")
    public Result add(@RequestBody CheckItem checkItem){
        try {
            System.out.println("进入检查项");
            checkItemService.add(checkItem);
//            System.out.println("");
        }catch (Exception e){
            System.out.println("检查项新增出现异常:"+e);
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS);
    }


    //分页查询
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")//权限校验
    @RequestMapping("/findPage")
    public PageResult findpage(@RequestBody QueryPageBean queryPageBean){
        System.out.println("进入分页请求");
//        Integer currentPage = queryPageBean.getCurrentPage();
//        Integer pageSize = queryPageBean.getPageSize();
//        String queryString = queryPageBean.getQueryString();
//        System.out.println(currentPage+pageSize+queryString);


//        try{
//            PageResult pageResult = checkItemService.pageQuery(
//                    queryPageBean.getCurrentPage(),
//                    queryPageBean.getPageSize(),
//                    queryPageBean.getQueryString()
//
//            );
//            return pageResult;
//
//        }catch (Exception e){
//            System.out.println("分页请求异常"+e);
//        }
        PageResult pageResult = checkItemService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());

        return pageResult;
    }



    //检查项删除
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")//权限校验
    @RequestMapping("/delete")
    public Result delete(Integer id){
        System.out.println("进入删除检查项...");
        try{
            checkItemService.deleteById(id);
        }catch (Exception e){
            System.out.println("删除检查项异常"+e);
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }

        return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    //查询编辑数据
    @RequestMapping("/findById")
    public Result findById(Integer id){
        System.out.println("进入检查项编辑框回显数据");
        try{
            CheckItem checkItem = checkItemService.findById(id);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);

        }catch (Exception e){
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }

    }

    //检查项编辑
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")//权限校验
    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem){
        System.out.println("进入编辑检查项提交");
        try{
            checkItemService.edit(checkItem);
            return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
        }catch (Exception e){
            return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);

        }

    }

    //查询所有
    @RequestMapping("/findAll")
    public Result findAll(){
        List<CheckItem> checkItemList = checkItemService.findAll();
        if (checkItemList != null && checkItemList.size() >0){
            Result result = new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS);
            result.setData(checkItemList);
            return result;
        }
        return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
    }



}
